package com.cunjin.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cunjin.admin.dao.AdminDao;
import com.cunjin.admin.entity.AdminEntity;
import com.cunjin.admin.service.AdminService;
import com.cunjin.base.service.impl.BaseServiceImpl;

import tk.mybatis.mapper.entity.Example;

@Service
public class AdminServiceImpl extends BaseServiceImpl<AdminEntity> implements AdminService {

	@Autowired
	private AdminDao dao;
	
	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> login(AdminEntity entity,HttpServletRequest req) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		AdminEntity user;
		try {
			user = dao.login(entity);
			if(null !=user){
				HttpSession session = req.getSession();
				String loginNo = entity.getLoginNo();
				String username = user.getName();
				session.setAttribute("loginNo", loginNo);
				session.setAttribute("username", username);
				map.put("code", "200");
				map.put("jg", "登陆成功");
				map.put("username", username);
			}else{
				map.put("code", "500");
				map.put("jg", "登陆失败");
			}
		} catch (Exception e) {
			map.put("code", "500");
			map.put("jg", "登陆失败");
			e.printStackTrace();
		}
		
		return map;
	}

	@Transactional(readOnly=true)
	@Override
	public Map<String, Object> queryByPage(int page, int rows, String searchName) {
		//将分页的数据保存在map中
		Map<String, Object> pages=new HashMap<String, Object>();
		
		int before=(page-1)*rows;
		int after=rows;
		pages.put("before", before);
		pages.put("after", after);
		if(null !=searchName){
			pages.put("searchName", searchName+"%");
		}else{
			pages.put("searchName", null);
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List<AdminEntity> ls=dao.myqueryByPage(pages);
			map.put("total",dao.findAllByName(pages));
			map.put("rows",ls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> insert(AdminEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//先判断是否重复
			String gradeLoginNo = entity.getLoginNo();
			Example example=new Example(AdminEntity.class);
			example.createCriteria().andCondition("LOGIN_NO="+gradeLoginNo);
			List<AdminEntity> selectByExample = dao.selectByExample(example);
			int size = selectByExample.size();
			//size==0说明没有个账号
			if(size==0){
				int count = dao.insert(entity);
				if(count==1){
					map.put("code", 200);
					map.put("msg","添加管理员成功");
				}
				else{
					map.put("code", 500);
				}
			}else{
				map.put("code", 201);
				map.put("msg","登陆账号"+gradeLoginNo+"已经存在");
			}
		} catch (Exception e) {
			map.put("code", 500);
			e.printStackTrace();
		}
		return map;
	}

	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> deleteByids(String[] split) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			
			int cs=0;
			for (String s : split) {
				int did = Integer.parseInt(s);
				dao.deleteByPrimaryKey(did);
				cs++;
			}
			map.put("code", 200);
			map.put("count", cs);
		} catch (Exception e) {
			map.put("code",500);
			e.printStackTrace();
		}
		return map;
	}
	
	@Transactional(readOnly=false)
	@Override
	public Map<String, Object> update(AdminEntity entity) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		
		//排除更新的账号重复性
		Integer oldId = entity.getId();
		String newLoginNo = entity.getLoginNo();
		AdminEntity oldentity = dao.selectByPrimaryKey(oldId);
		String oldLoginNo = oldentity.getLoginNo();
		//如果是两个相等没有账号
		if(newLoginNo.equals(oldLoginNo)){
			dao.updateByPrimaryKeySelective(entity);
			map.put("code", 200);
			map.put("msg","更新成功");
			return map;
		}else{
			//修改了账号名称
			try {
				Example example=new Example(AdminEntity.class);
				example.createCriteria().andCondition("LOGIN_NO="+newLoginNo);
				 List<AdminEntity> selectByExample = dao.selectByExample(example);
				//==0 说明没有这个宿舍
				if(selectByExample.size()==0){
					dao.updateByPrimaryKeySelective(entity);
				}else{
					map.put("code", 201);
					map.put("msg", "该账号"+newLoginNo+"已经存在");
					return map;
				}
			} catch (Exception e) {
				map.put("code", 500);
				map.put("msg","更新失败");
				e.printStackTrace();
				return map;
			}
		}
		return map;
	}

	@Override
	public Map<String, Object> regUser(AdminEntity entity) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<AdminEntity> select = dao.select(entity);
		int size = select.size();
		if(size==0){
			int i = dao.insert(entity);
			map.put("code", 200);
			map.put("msg","更新成功");
		}else{
			map.put("code", 201);
			map.put("msg", "该账号已经存在");
		}
		return map;
	}
}
